package com.ctlele.team.space.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class BaseFragment extends Fragment {

    protected View mRootView;

    private boolean mIsLoaded = false;

    protected abstract int initLayout();

    protected abstract void initView();

    protected abstract void initData();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(initLayout(), container, false);
            initView();
        }
        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!mIsLoaded) {
            initData();
            mIsLoaded = true;
        }
    }

    protected void turnTo(Class<?> to) {
        Intent intent = new Intent(getActivity(), to);
        startActivity(intent);
    }

    public void reload() {
        mIsLoaded = false;
    }
}
